package com.erp.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.erp.order.mapper.ErpSellerOrderLogMapper;
import com.erp.order.mapper.ErpSellerOrderMapper;
import com.erp.order.service.IAeOrderService;
import com.erp.order.service.IAeSaveOrderService;
import com.erp.order.service.PullOrderFromAeService;
import com.ruoyi.ae.api.RemoteAeOrderService;
import com.ruoyi.ae.api.domain.AeStoreChildOrder;
import com.ruoyi.ae.api.domain.AeStoreOrder;
import com.ruoyi.ae.api.domain.AeStoreOrderProduct;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.order.api.domain.ErpSellerOrder;
import com.ruoyi.order.api.domain.ErpSellerOrderLog;
import com.ruoyi.order.api.domain.ErpSellerOrderProduct;
import com.ruoyi.shop.api.RemoteSellerStoreService;
import com.ruoyi.shop.api.domain.ErpSellerStoreEmpower;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RequiredArgsConstructor
@Service
public class PullOrderFromAeServiceImpl implements PullOrderFromAeService {
    @Resource
    ErpSellerOrderLogMapper erpSellerOrderLogMapper;

    /**
     * 问题单返回备注
     *
     * @param orderStatus
     * @return
     */
    @Override
    public String problemRemark(String orderStatus) {
        String remark = null;
        switch (orderStatus) {
            case "IN_CANCEL":
                remark = "买家申请取消";
                break;
            case "FUND_PROCESSING":
                remark = "买卖家达成一致，资金处理中";
                break;
            case "IN_ISSUE":
                remark = "含纠纷中的订单";
                break;
            case "IN_FROZEN":
                remark = "冻结中的订单";
                break;
            case "WAIT_SELLER_EXAMINE_MONEY":
                remark = "等待卖家确认金额";
                break;
            default:
                remark = "其他问题(" + orderStatus + ")";
                break;
        }
        return remark;
    }

    /**
     * 获取该订单的上一个流程代码
     *
     * @param orderId
     */
    @Override
    public ErpSellerOrderLog findLastOrderLog(Long orderId) {
        List<ErpSellerOrderLog> logList = erpSellerOrderLogMapper.selectList(
                new LambdaQueryWrapper<ErpSellerOrderLog>()
                        .eq(ErpSellerOrderLog::getOrderId, orderId)
                        .orderByDesc(ErpSellerOrderLog::getCreateTime)
        );
        String previousStatus = null;
        for (ErpSellerOrderLog log : logList) {
            //查看日志的流程代码
            previousStatus = log.getPreviousStatus();
            if (previousStatus.equals("problem")) {
                continue;
            }
            return log;
        }
        return null;
    }

    @Override
    public ErpSellerOrder encapsulationErpOrder( Map<String, Object> divideMap) {
        ErpSellerOrder erpSellerOrder = new ErpSellerOrder();
        AeStoreOrder aeStoreOrder = JSONObject.parseObject(divideMap.get("newOrder").toString(), AeStoreOrder.class);
        //获取对应的数据
        erpSellerOrder.setSellerLoginId(aeStoreOrder.getSellerOperatorLoginId());
        erpSellerOrder.setLeftEndGoodTime(aeStoreOrder.getLeftEndGoodTime());
        erpSellerOrder.setSellerOperatorLoginId(aeStoreOrder.getSellerOperatorLoginId());
        erpSellerOrder.setSellerSignerFullname(aeStoreOrder.getSellerSignerFullname());
        erpSellerOrder.setBuyerLoginId(aeStoreOrder.getBuyerLoginId());
        erpSellerOrder.setOrderId(aeStoreOrder.getOrderId());
        erpSellerOrder.setBizType(aeStoreOrder.getBizType());
        erpSellerOrder.setBuyerSignerFullname(aeStoreOrder.getBuyerSignerFullname());
        erpSellerOrder.setGmtCreate(aeStoreOrder.getGmtCreate());
        erpSellerOrder.setGmtPayTime(aeStoreOrder.getGmtPayTime());
        erpSellerOrder.setGmtUpdate(aeStoreOrder.getGmtUpdate());
        erpSellerOrder.setOrderDetailUrl(aeStoreOrder.getOrderDetailUrl());
        erpSellerOrder.setOrderStatus(aeStoreOrder.getOrderStatus());
        erpSellerOrder.setPayCurrencyCode(aeStoreOrder.getPayCurrencyCode());
        erpSellerOrder.setPayAmount(aeStoreOrder.getSellerOrderAmount());
        //获取子订单Id列表
        List<AeStoreChildOrder> childOrderIdList = JSONObject.parseArray(divideMap.get("newChildOrderList").toString(), AeStoreChildOrder.class);
        //遍历该List，获取商品List
        List<ErpSellerOrderProduct> erpSellerOrderProductList = new ArrayList<>();
        erpSellerOrder.setAeStoreChildOrderList(childOrderIdList);
        for (AeStoreChildOrder childOrder : childOrderIdList) {
            //通过子订单id，获取订单商品信息
            //遍历订单商品id，并存储
            List<AeStoreOrderProduct> aeStoreOrderProductList = JSONObject.parseArray(divideMap.get("newOrderProductList").toString(), AeStoreOrderProduct.class);
            for (AeStoreOrderProduct aeStoreOrderProduct : aeStoreOrderProductList) {
                ErpSellerOrderProduct erpProduct = new ErpSellerOrderProduct();
                //查找该订单下的子订单的商品详情
                if (aeStoreOrderProduct != null) {
                    erpProduct.setOrderId(erpSellerOrder.getOrderId());
                    erpProduct.setProductId(aeStoreOrderProduct.getProductId());
                    erpProduct.setChildId(aeStoreOrderProduct.getChildId());
                    erpProduct.setChildLogisticsAmount(aeStoreOrderProduct.getChildLogisticsAmount());
                    erpProduct.setChildLogisticsCurrencyCode(aeStoreOrderProduct.getChildLogisticsCurrencyCode());
                    erpProduct.setMemo(aeStoreOrderProduct.getMemo());
                    erpProduct.setProductCount(aeStoreOrderProduct.getProductCount());
                    erpProduct.setProductId(aeStoreOrderProduct.getProductId());
                    erpProduct.setProductImgUrl(aeStoreOrderProduct.getProductImgUrl());
                    erpProduct.setProductName(aeStoreOrderProduct.getProductName());
                    erpProduct.setProductSnapUrl(aeStoreOrderProduct.getProductSnapUrl());
                    erpProduct.setProductStandard(aeStoreOrderProduct.getProductStandard());
                    erpProduct.setProductUnit(aeStoreOrderProduct.getProductUnit());
                    erpProduct.setProductUnitPrice(aeStoreOrderProduct.getProductUnitPrice());
//                        erpProduct.setProductUnitCurrencyCode(aeCurrencyDescriptionMapper.selectOne(new LambdaQueryWrapper<AeCurrencyDescription>().eq(AeCurrencyDescription::getId, aeStoreOrderProduct.getProductCurrencyDescriptionId())).getCurrencyCode());
                    erpProduct.setShowStatus(aeStoreOrderProduct.getShowStatus());
                    erpProduct.setSkuCode(aeStoreOrderProduct.getSkuCode());
                    erpProduct.setSonOrderStatus(aeStoreOrderProduct.getSonOrderStatus());
                    erpProduct.setTotalProductAmount(aeStoreOrderProduct.getTotalProductAmount());
                    erpProduct.setTotalProductCurrencyCode(aeStoreOrderProduct.getTotalProductCurrencyCode());
                }
                //信息遍历完后，进行存储
                erpSellerOrderProductList.add(erpProduct);
            }
        }
        erpSellerOrder.setSellerOrderProductList(erpSellerOrderProductList);

        return erpSellerOrder;
    }

}

